﻿/*!	@file	r_raytrace_render.cpp
	@brief	raytraceレンダリングシステム
Copyright (c) 2011 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "r_raytrace_render.h"
#include "r_raytrace_buffer.h"
#include "r_raytrace_camera.h"
#include "r_raytrace_scene.h"

#include "../math/r_math_ray.h"

namespace r{
	namespace raytrace{

		Render::Render( void )
		{
		}
		
		Render::~Render( void )
		{
		}

		void Render::beginRender( Scene* scene, Buffer* buffer, const Camera& camera )
		{
			// 初期化を行う
			// 描画スレッドを開始する

			// としたいところだけどまずは実装テスト
			r_ui32 h = buffer->getHeight();
			r_ui32 w = buffer->getWidth();

			r_f32 yStep = 2.0f / h;
			r_f32 xStep = 2.0f / w;
			r_f32 yPos = -1.0f;
			r::math::Ray ray;
			const r::math::Matrix34& locale = camera.getLocale();
			ray.position.x = locale._31;
			ray.position.y = locale._32;
			ray.position.z = locale._33;
			for( r_ui32 y = 0; y < h; y++ ){
				r_f32 xPos = -1.0f;
				for( r_ui32 x = 0; x < w; x++ ){
					ray.direction = camera.getEyeVector( xPos, yPos );
					r_f32 result = scene->rayCast( ray );
					if( result > 0.0f ){
						BufferPixel pix;
						buffer->setPixel( x, y, pix );
					}
					xPos += xStep;
				}
				yPos += yStep;
			}
		}
		
		r_bool Render::checkRender( void )
		{
			return false;
		}
		
		void Render::stopRender( void )
		{
			// 動作中のスレッドを中止する
		}
	}
}


